package models

import (
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

func GetDeport() (error, []deport, int64) { //获取仓库信息包括仓库号和剩余容量
	var port []deport
	var total int64
	err := db.Table("deports").Count(&total).Order("did ASC").Find(&port).Error //数据库查询操作
	return err, port, total
}

func GetDetail(storgeParam map[string]interface{}) (error, []Storage, int64) {
	var storgedata []Storage
	page := storgeParam["page"].(int)                //页数
	pageSize := storgeParam["limit"].(int)           //每页个数
	searchText := storgeParam["searchText"].(string) //查询的信息(在这里就是仓库号）
	fmt.Println(page, pageSize, searchText)
	var total int64

	err := db.Table("storage").Where("did like ?", "%"+searchText+"%").Order("in_time ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&storgedata).Error
	return err, storgedata, total
}

func Add(c *gin.Context) (error, Storage) { //向仓库添加货物
	var storagedata Storage
	var a deport
	var b Good
	c.ShouldBindJSON(&storagedata)

	err := db.Table("goods").Where("gid=?", storagedata.Gid).Find(&b).Error //获取对应商品数量
	if err != nil {
		return err, Storage{}
	}
	storagedata.Num = b.Num
	storagedata.InTime = time.Now()
	err = db.Table("storage").Select("sid", "gid", "num", "did", "in_time").Create(&storagedata).Error //创建仓储信息条目
	if err == nil {
		err = db.Table("deports").Where("did = ?", storagedata.Did).Find(&a).Error //更新仓库信息
		if err != nil {
			return err, Storage{}
		}
		a.Volume = a.Volume - storagedata.Num
		if a.Volume < 0 {
			db.Table("storage").Where("sid = ?", storagedata.Sid).Delete(&storagedata)
			return errors.New("仓库放不下啦"), Storage{}
		}
		err = db.Table("deports").Where("did=?", storagedata.Did).Update("volume", a.Volume).Error
		if err != nil {
			return err, Storage{}
		}
		err = db.Table("goods").Where("gid=?", storagedata.Gid).Update("state", true).Error
	}
	return err, storagedata
}

func Take(c *gin.Context) (error, Storage) { //出库的数据库处理
	var storagedata Storage
	var outData out_load
	var order Order
	var dep deport
	c.ShouldBindJSON(&outData) //前端数据和结构体绑定
	//if err != nil {
	//	return err, Storage{}
	//}
	err := db.Table("orders").Where("oid = ?", outData.Oid).Find(&order).Error
	if err != nil {
		return err, Storage{}
	}
	outData.OutTime = time.Now()
	err1 := db.Table("out_load").Select("id", "out_time", "num", "sid", "oid").Create(&outData).Error //创建一条出库信息
	if err1 == nil {
		err1 = db.Table("storage").Where("sid = ?", outData.Sid).Find(&storagedata).Error //找出要更新的仓储信息
		if err1 == nil {
			storagedata.Num = storagedata.Num - outData.Num //更新仓储信息
			if storagedata.Num < 0 {
				db.Table("out_load").Where("id = ?").Delete(&outData)
				return errors.New("仓储不足"), Storage{}
			}

			db.Table("storage").Where("sid = ?", storagedata.Sid).Updates(&storagedata)
			db.Table("deport").Where("did=?", storagedata.Did).Find(&dep)
			dep.Volume = dep.Volume + outData.Num
			db.Table("deport").Where("did=?", dep.Did).Update("volume", dep.Volume)
			day := (outData.OutTime.Sub(storagedata.InTime).Hours()) / 24
			days := float32(day)
			var fee float32
			if days > 15 {
				fee = ((days - 14) * 0.5) * outData.Num
			} else {
				fee = 0
			}
			db.Table("out_load").Where("id = ?", outData.Id).Update("fee", fee)
		}
	}
	return err1, storagedata
}

func Getoutload(storgeParam map[string]interface{}) (error, []out_load) {
	searchText := storgeParam["searchText"].(string)
	var out []out_load
	err := db.Table("out_load").Where("oid like ?", "%"+searchText+"%").Find(&out).Error
	return err, out
}
